package com.wcpdoc.exam.core.service.impl;

import com.wcpdoc.base.util.CurLoginUserUtil;
import com.wcpdoc.core.dao.BaseDao;
import com.wcpdoc.core.exception.MyException;
import com.wcpdoc.core.service.impl.BaseServiceImp;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.dao.MyCollectDao;
import com.wcpdoc.exam.core.entity.MyCollect;
import com.wcpdoc.exam.core.service.MyCollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收藏服务层实现
 * 
 * v1.0 chenyun 2021-03-02 13:43:21
 */
@Service
public class MyCollectServiceImpl extends BaseServiceImp<MyCollect> implements MyCollectService {
	@Resource
	private MyCollectDao myCollectDao;
	@Override
	@Resource(name = "myCollectDaoImpl")
	public void setDao(BaseDao<MyCollect> dao) {
		super.dao = dao;
	}

	@Override
	public void addCollect(MyCollect myCollect) {
		// 数据校验
		addValid(myCollect);
		MyCollect entity = getByQuestion(myCollect.getQuestionId(),myCollect.getUserId(),myCollect.getType());
		if(entity!=null){
			// 收藏添加
			myCollect.setId(entity.getId());
			updateCollect(myCollect);
		}else {
			// 收藏添加
			myCollect.setUpdateUserId(getCurUser().getId());
			myCollect.setUpdateTime(new Date());
			myCollect.setState(1);
			add(myCollect);
		}
	}

	@Override
	public void updateCollect(MyCollect myCollect) {
		// 数据校验
		MyCollect entity = getEntity(myCollect.getId());
		updateValid(myCollect, entity);
		
		// 收藏修改
		entity.setQuestionId(myCollect.getQuestionId());
		entity.setUserId(myCollect.getUserId());
		//entity.setState(myCollect.getState());
		entity.setType(myCollect.getType());
		entity.setUserAnswer(myCollect.getUserAnswer());
		entity.setUpdateUserId(getCurUser().getId());
		entity.setUpdateTime(new Date());
		update(entity);
	}

	private void updateValid(MyCollect myCollect, MyCollect entity) {
		if (!ValidateUtil.isValid(myCollect.getId())) {
			throw new MyException("参数错误：id");
		}
		if (!ValidateUtil.isValid(myCollect.getQuestionId())) {
			throw new MyException("参数错误：questionId");
		}
		if (!ValidateUtil.isValid(myCollect.getUserId())) {
			throw new MyException("参数错误：userId");
		}
		if (!ValidateUtil.isValid(myCollect.getType())) {
			throw new MyException("参数错误：type");
		}
		if (entity.getState() == 0) {
			throw new MyException("已删除");
		}
//		if (entity.getEndTime().getTime() <= System.currentTimeMillis()) {
//			throw new MyException("收藏已结束");
//		}
//		if (entity.getStartTime().getTime() <= System.currentTimeMillis()) {
//			throw new MyException("收藏已开始");// 考试结束放在考试开始前校验，可能的问题为考试已结束，提示的是考试已开始。
//		}
		if (!(CurLoginUserUtil.isSelf(entity.getUpdateUserId()) || CurLoginUserUtil.isAdmin())) {
			throw new MyException("无操作权限");
		}
//		QuestionType questionType = questionTypeService.getEntity(exer.getQuestionTypeId());// 只能收藏自己的题库
//		if (!(CurLoginUserUtil.isSelf(questionType.getCreateUserId()) || CurLoginUserUtil.isAdmin())) {
//			throw new MyException("无操作权限");
//		}
//		if (getCurUser().getType() != 0) {
//			User curUser = userService.getEntity(getCurUser().getId());
//			Set<Integer> haveUsers = CollectionUtil.toSet(curUser.getUserIds());
//			Set<Integer> exerUsers = CollectionUtil.toSet(exer.getUserIds());
//			if (!haveUsers.containsAll(exerUsers)) {// 只能收藏自己的考试用户
//				throw new MyException("无操作权限");
//			}
//		}
		
//		List<MyCollect> exerList = exerDao.getList(exer.getQuestionTypeId());
//		for (MyCollect cur : exerList) {
//
//			if (cur.getId().intValue() == exer.getId().intValue()) {// 如果变更题库，该行无效；如果同一个题库，校验的时候排除自己。
//				continue;
//			}
//			if (exer.getStartTime().getTime() <= cur.getStartTime().getTime() && cur.getStartTime().getTime() <= exer.getEndTime().getTime()) {
//				throw new MyException("该时间段已存在");
//			}
//			if (exer.getStartTime().getTime() <= cur.getEndTime().getTime() && cur.getEndTime().getTime() <= exer.getEndTime().getTime()) {
//				throw new MyException("该时间段已存在");
//			}
//			if (exer.getStartTime().getTime() >= cur.getStartTime().getTime() && cur.getEndTime().getTime() >= exer.getEndTime().getTime() ) {
//				throw new MyException("该时间段已存在");
//			}
//		}
	}
	
	private void addValid(MyCollect myCollect) {
		if (!ValidateUtil.isValid(myCollect.getQuestionId())) {
			throw new MyException("参数错误：questionId");
		}
		if (!ValidateUtil.isValid(myCollect.getUserId())) {
			throw new MyException("参数错误：userId");
		}
		if (!ValidateUtil.isValid(myCollect.getType())) {
			throw new MyException("参数错误：type");
		}
		if (!ValidateUtil.isValid(myCollect.getQuestionTypeId())) {
			throw new MyException("参数错误：qtypeId");
		}
		if (!(CurLoginUserUtil.isSelf(myCollect.getUserId()) || CurLoginUserUtil.isAdmin())) {
			throw new MyException("无操作权限");
		}

//		QuestionType questionType = questionTypeService.getEntity(exer.getQuestionTypeId());// 只能收藏自己的题库
//		if (!(CurLoginUserUtil.isSelf(questionType.getCreateUserId()) || CurLoginUserUtil.isAdmin())) {
//			throw new MyException("无操作权限");
//		}
//		if (getCurUser().getType() != 0) {
//			User curUser = userService.getEntity(getCurUser().getId());
//			Set<Integer> haveUsers = CollectionUtil.toSet(curUser.getUserIds());
//			Set<Integer> exerUsers = CollectionUtil.toSet(exer.getUserIds());
//			if (!haveUsers.containsAll(exerUsers)) {// 只能收藏自己的考试用户
//				throw new MyException("无操作权限");
//			}
//		}
//
//		List<MyCollect> exerList = exerDao.getList(exer.getQuestionTypeId());
//		for (MyCollect cur : exerList) {
//			if (cur.getCreateUserId().intValue() != getCurUser().getId().intValue()) {// 管理员和子管理，同一时间创建同一个题库，提示了错误但是相互看不见。改为自己的不允许重复
//				continue;
//			}
//
//			if (exer.getStartTime().getTime() <= cur.getStartTime().getTime() && cur.getStartTime().getTime() <= exer.getEndTime().getTime()) {
//				throw new MyException("该时间段已存在");
//			}
//			if (exer.getStartTime().getTime() <= cur.getEndTime().getTime() && cur.getEndTime().getTime() <= exer.getEndTime().getTime()) {
//				throw new MyException("该时间段已存在");
//			}
//			if (exer.getStartTime().getTime() >= cur.getStartTime().getTime() && cur.getEndTime().getTime() >= exer.getEndTime().getTime() ) {
//				throw new MyException("该时间段已存在");
//			}
//		}
	}

	@Override
	public List<MyCollect> getList(Integer questionTypeId) {
		return myCollectDao.getList(questionTypeId);
	}

	@Override
	public MyCollect getByQuestion(Integer questionId, Integer userId,Integer type) {
		return myCollectDao.getByQuestion(questionId,userId,type);
	}

	@Override
	public void delByQuestionId(Integer questionId, Integer userId, Integer type) {
		myCollectDao.delByQuestionId(questionId,userId,type);
	}

	@Override
	public List<MyCollect> getIds(Integer questionTypeId,Integer type,Integer userId) {
		return myCollectDao.getIds(questionTypeId,type,userId);
	}

	@Override
	public List<Map<String,Object>> getPaperByType(Integer type, Integer userId,Integer questionTypeId) {
		return myCollectDao.getPaperByType(type,userId,questionTypeId);
	}
}
